{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# face++ 人脸检测/face detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.输入我们需要的API网站注册的API_Key\n",
    "API_Key = \"IA_NDc4jmNFEc23gAr6gkrHm-2WXJ3_T\"\n",
    "API_secret = \"Q7IfcDG_0iC8vjm220qCDyTzZ_SnhJ7j\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.准备API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4.调用方法 POST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5.准备url传递参数\n",
    "payload ={\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://pica.zhimg.com/80/v2-fc989e7f110a62d450d2f3bd8f6b15c9_720w.jpg?source=1940ef5c\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6.发起请求\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648271362,d8158c3a-4ccc-46b8-a7f6-9b2d4424b10b',\n",
       " 'time_used': 358,\n",
       " 'faces': [{'face_token': '84d59ccca27a76719c188afc921b6784',\n",
       "   'face_rectangle': {'top': 138, 'left': 292, 'width': 142, 'height': 142},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 19},\n",
       "    'smile': {'value': 71.133, 'threshold': 50.0}}}],\n",
       " 'image_id': 'doBw3we5JL1GQZbr7IeDzA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"IA_NDc4jmNFEc23gAr6gkrHm-2WXJ3_T\"\n",
    "API_secret = \"Q7IfcDG_0iC8vjm220qCDyTzZ_SnhJ7j\"\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':\"IA_NDc4jmNFEc23gAr6gkrHm-2WXJ3_T\",\n",
    "    'api_secret':\"Q7IfcDG_0iC8vjm220qCDyTzZ_SnhJ7j\",\n",
    "    'face_tokens':'84d59ccca27a76719c188afc921b6784',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 180,\n",
       " 'request_id': '1648271569,e99f3959-9969-45a7-b8fc-ccfc4eca9347',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.197,\n",
       "     'neutral': 57.992,\n",
       "     'disgust': 0.094,\n",
       "     'anger': 0.111,\n",
       "     'surprise': 0.042,\n",
       "     'fear': 0.065,\n",
       "     'happiness': 41.499},\n",
       "    'beauty': {'female_score': 74.606, 'male_score': 72.428},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 19},\n",
       "    'mouthstatus': {'close': 99.987,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.013,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 3.535,\n",
       "     'stain': 6.94,\n",
       "     'acne': 8.793,\n",
       "     'health': 36.774},\n",
       "    'headpose': {'yaw_angle': 5.3845305,\n",
       "     'pitch_angle': -6.5816903,\n",
       "     'roll_angle': -0.5624117},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.873},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.873},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.873}},\n",
       "    'smile': {'threshold': 50.0, 'value': 71.133},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.352,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.043,\n",
       "      'no_glass_eye_open': 99.595,\n",
       "      'normal_glass_eye_close': 0.001,\n",
       "      'dark_glasses': 0.009},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.02,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.979,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 88.668},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.479,\n",
       "      'vector_z_component': 0.979,\n",
       "      'vector_x_component': -0.107,\n",
       "      'vector_y_component': 0.171,\n",
       "      'position_y_coordinate': 0.432},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.484,\n",
       "      'vector_z_component': 0.976,\n",
       "      'vector_x_component': 0.14,\n",
       "      'vector_y_component': 0.168,\n",
       "      'position_y_coordinate': 0.425}}},\n",
       "   'face_rectangle': {'width': 142, 'top': 138, 'left': 292, 'height': 142},\n",
       "   'face_token': '84d59ccca27a76719c188afc921b6784'}]}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>84d59ccca27a76719c188afc921b6784</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>57.992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>0.094</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>41.499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>74.606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>72.428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>99.987</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>0.013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>3.535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>6.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>8.793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>36.774</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>5.38453</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>-6.58169</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>-0.562412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>71.133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.595</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>88.668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>0.171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.484</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>0.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>0.168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>292</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          84d59ccca27a76719c188afc921b6784\n",
       "attributes.emotion.sadness                                                     0.197\n",
       "attributes.emotion.neutral                                                    57.992\n",
       "attributes.emotion.disgust                                                     0.094\n",
       "attributes.emotion.anger                                                       0.111\n",
       "attributes.emotion.surprise                                                    0.042\n",
       "attributes.emotion.fear                                                        0.065\n",
       "attributes.emotion.happiness                                                  41.499\n",
       "attributes.beauty.female_score                                                74.606\n",
       "attributes.beauty.male_score                                                  72.428\n",
       "attributes.gender.value                                                         Male\n",
       "attributes.age.value                                                              19\n",
       "attributes.mouthstatus.close                                                  99.987\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                                 0\n",
       "attributes.mouthstatus.open                                                    0.013\n",
       "attributes.mouthstatus.other_occlusion                                             0\n",
       "attributes.glass.value                                                          None\n",
       "attributes.skinstatus.dark_circle                                              3.535\n",
       "attributes.skinstatus.stain                                                     6.94\n",
       "attributes.skinstatus.acne                                                     8.793\n",
       "attributes.skinstatus.health                                                  36.774\n",
       "attributes.headpose.yaw_angle                                                5.38453\n",
       "attributes.headpose.pitch_angle                                             -6.58169\n",
       "attributes.headpose.roll_angle                                             -0.562412\n",
       "attributes.blur.blurness.threshold                                                50\n",
       "attributes.blur.blurness.value                                                 0.873\n",
       "attributes.blur.motionblur.threshold                                              50\n",
       "attributes.blur.motionblur.value                                               0.873\n",
       "attributes.blur.gaussianblur.threshold                                            50\n",
       "attributes.blur.gaussianblur.value                                             0.873\n",
       "attributes.smile.threshold                                                        50\n",
       "attributes.smile.value                                                        71.133\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                             0.352\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                                 0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                 0.043\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                            99.595\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                             0.001\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                              0.009\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                              0.02\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                                 0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                    0\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                            99.979\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                                 0\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                                 0\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  88.668\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.479\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.979\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.107\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                             0.171\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.432\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.484\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.976\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                              0.14\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                             0.168\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.425\n",
       "face_rectangle.width                                                             142\n",
       "face_rectangle.top                                                               138\n",
       "face_rectangle.left                                                              292\n",
       "face_rectangle.height                                                            142"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r_analyze.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_Key = \"IA_NDc4jmNFEc23gAr6gkrHm-2WXJ3_T\"\n",
    "API_secret = \"Q7IfcDG_0iC8vjm220qCDyTzZ_SnhJ7j\"\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "payload ={\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202003%2F18%2F20200318131532_enika.thumb.400_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650865038&t=993999bd4a9b810fe29f928c881b2183\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648273277,73400c86-78e1-4283-b692-76498d98eaad',\n",
       " 'time_used': 223,\n",
       " 'faces': [{'face_token': '67478be514b1103b1fa53c4b7ad4fd2d',\n",
       "   'face_rectangle': {'top': 90, 'left': 127, 'width': 143, 'height': 143},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 57},\n",
       "    'smile': {'value': 0.0, 'threshold': 50.0}}}],\n",
       " 'image_id': '7WDN2SbdkIHilRqxbCfz9Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/compare\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"IA_NDc4jmNFEc23gAr6gkrHm-2WXJ3_T\"\n",
    "API_secret = \"Q7IfcDG_0iC8vjm220qCDyTzZ_SnhJ7j\"\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':\"IA_NDc4jmNFEc23gAr6gkrHm-2WXJ3_T\",\n",
    "    'api_secret':\"Q7IfcDG_0iC8vjm220qCDyTzZ_SnhJ7j\",\n",
    "    'face_tokens1':'84d59ccca27a76719c188afc921b6784',\n",
    "    'face_tokens2':'67478be514b1103b1fa53c4b7ad4fd2d',\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [404]>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 这里的404还在寻找原因"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_message': 'API_NOT_FOUND'}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.封装应用函数（内部使用的接口）\n",
    "## 1.人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "api_key = \"IA_NDc4jmNFEc23gAr6gkrHm-2WXJ3_T\"\n",
    "api_secret = \"Q7IfcDG_0iC8vjm220qCDyTzZ_SnhJ7j\"\n",
    "img_url_01 = 'https://pica.zhimg.com/80/v2-fc989e7f110a62d450d2f3bd8f6b15c9_720w.jpg?source=1940ef5c'\n",
    "img_url_02 = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202003%2F18%2F20200318131532_enika.thumb.400_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650865038&t=993999bd4a9b810fe29f928c881b2183'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(api_key,api_secret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':\"IA_NDc4jmNFEc23gAr6gkrHm-2WXJ3_T\",\n",
    "        'api_secret':\"Q7IfcDG_0iC8vjm220qCDyTzZ_SnhJ7j\",\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648274350,9aec53fa-53c4-4989-84b9-72669c61a37a',\n",
       " 'time_used': 306,\n",
       " 'faces': [{'face_token': '6f30824bc232691a3af62a82e3e3b841',\n",
       "   'face_rectangle': {'top': 138, 'left': 292, 'width': 142, 'height': 142}}],\n",
       " 'image_id': 'doBw3we5JL1GQZbr7IeDzA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(api_key,api_secret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648274353,2977eb2d-dbfd-4853-90c7-8539b97fff99',\n",
       " 'time_used': 107,\n",
       " 'faces': [{'face_token': '893ec1e389268ea31ffe862d655ebf02',\n",
       "   'face_rectangle': {'top': 90, 'left': 127, 'width': 143, 'height': 143}}],\n",
       " 'image_id': '7WDN2SbdkIHilRqxbCfz9Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(api_key,api_secret,img_url_02)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
